//
//  InputVerificationCodeView.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/4/9.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "InputVerificationCodeView.h"
#include <UIView+TYAlertView.h>

@interface InputVerificationCodeView()

@property (nonatomic,copy) NSString *phone;
@property (nonatomic,weak) UITextField *inputTextField;
@property (nonatomic,weak) CountDownButton *countDownBtn;

@end


@implementation InputVerificationCodeView

- (instancetype)initWithPhone:(NSString *)phone{
    if (self = [super init]) {
        self.backgroundColor=[UIColor whiteColor];
        AvoidNullString(phone);
        self.phone = phone;
        self.layer.cornerRadius = 12.0;
        self.layer.masksToBounds = YES;
        self.frame = CGRectMake(0, 0, 316, 188);
        [self setupSubviews];
    }
    return self;
}

- (void)dealloc{
    LogDealloc
}


- (void)beginCountDown{
    [self.countDownBtn  beginTimeWithCompletion:^(CountDownButton *countDownButton) {
        
    }];
}



- (void)clickAction{
    [self.countDownBtn  beginTimeWithCompletion:^(CountDownButton *countDownButton) {
        
    }];
    if(self.retryHandler){
        self.retryHandler();
    }
}


- (void)textFieldDidChange:(UITextField *)textField{
//    if (textField.text.length > 1) {
//        textField.text = [textField.text substringToIndex:1];
//    }
}



- (void)setupSubviews{
    UILabel *titleLbl = [[UILabel alloc]init];
    NSString *startPhone = [CedarTools replacingMiddleStarByPhone:self.phone];
    NSString *text = [NSString stringWithFormat:@"已向您手机%@发送验证码",startPhone];
    MACRO_UILabel_Configure(titleLbl, text, kBlackColor, 17);
    titleLbl.numberOfLines = 0;
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(1);
        make.right.equalTo(self).offset(-1);
        make.top.equalTo(self).offset(25);
    }];
    
    UITextField *textField =  [[UITextField alloc]init];
    textField.placeholder = @"请输入验证码";
    textField.layer.borderColor = kSeparateLineColor.CGColor;
    textField.layer.borderWidth= 0.5f;
    textField.textAlignment = NSTextAlignmentCenter;
    [UIConfigure configureUITextFieldPlaceholderCenter:textField fontSize:15 color:kPlaceHolderColor];
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:textField];
    self.inputTextField = textField;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(28);
        make.top.equalTo(titleLbl.mas_bottom).offset(30);
        make.size.mas_offset(CGSizeMake(153, 45));
    }];
    
    NSInteger secondsTime = 60;
    CountDownButton *countDownBtn  =   [CountDownButton buttonWithTime:secondsTime title:@"输入验证码"];
    [countDownBtn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    self.countDownBtn = countDownBtn;
    [countDownBtn PFCGStyleFontSize:15];
    [self addSubview:countDownBtn];
    [countDownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textField.mas_right).mas_offset(5);
        make.centerY.equalTo(textField);
        make.size.mas_offset(CGSizeMake(115, 45));
    }];
    
    UIView *hLineView =  [[UIView alloc]init];
    hLineView.backgroundColor = kSeparateLineColor;
    [self addSubview:hLineView];
    [hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(textField.mas_bottom).mas_offset(15);
        make.height.mas_offset(0.5);
    }];
    
    CedarButton *cancelBtn =  [CedarButton buttonWithTextNoStytle:@"取消"];
    @weakify(self)
    cancelBtn.clickEvent = ^{
        @strongify(self)
        [self.countDownBtn stopTimer];
        [self hideView];
    };
    [cancelBtn PFCGStyleTextColor:kGrayColor fontSize:18.0];
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).mas_offset(61);
        make.top.mas_equalTo(hLineView).mas_offset(16);
        make.bottom.mas_equalTo(self).mas_offset(-16);
    }];
    
    UIView *vLineView =  [[UIView alloc]init];
    vLineView.backgroundColor = kSeparateLineColor;
    [self addSubview:vLineView];
    [vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(hLineView).mas_offset(10);
        make.bottom.mas_equalTo(self).mas_offset(-10);
        make.width.mas_offset(0.5);
    }];
    
    CedarButton *confirmBtn =  [CedarButton buttonWithTextNoStytle:@"确定"];
    confirmBtn.clickEvent = ^{
        @strongify(self)
        if (self.confirmHandelr) {
             //[self.countDownBtn stopTimer];
             CLog(@"confirm code:%@",self.inputTextField.text);
             self.confirmHandelr(self.inputTextField.text);
        }
        if(!IsEmptyStr(self.inputTextField.text)){
             [self.inputTextField resignFirstResponder];
        }
    };
    [confirmBtn PFCGStyleTextColor:kMasterColor fontSize:18.0];
    [self addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).mas_offset(-61);
        make.centerY.equalTo(cancelBtn);
    }];
}

- (void)stopCountDown{
    [self.countDownBtn stopTimer];
}


- (void)hideAlertView{
    [self hideView];
}

@end
